package com.guijianpan.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.guijianpan.demo.domain.Demo;
import com.guijianpan.demo.service.DemoService;
import com.guijianpan.framework.controller.BaseController;

@Controller
public class DemoController extends BaseController {
	
	@Autowired
	private DemoService demoService;
	
	@RequestMapping("/demo/findAll")
	public ModelAndView hello(String name) {
		return new ModelAndView("demo/demos").addObject("demos", demoService.findAll());
	}

	@RequestMapping("/demo/save")
	public ModelAndView save(String name) {
		Demo u = new Demo();
		u.setName(name);
		return new ModelAndView("demo/demos").addObject("demos", demoService.save(u));
	}

	@RequestMapping("/demo/updateNameById")
	public ModelAndView updateNameById(Long id, String name) {
		return new ModelAndView("demo/demos").addObject("demos", demoService.updateNameById(id, name));
	}

	@RequestMapping("/demo/del")
	public ModelAndView del(Long id) {
		return new ModelAndView("demo/demos").addObject("demos", demoService.del(id));
	}
	
	@RequestMapping("/demo/findByCondition")
	public ModelAndView findByCondition(String isDeleted, String name) {
		Demo demo = new Demo();
		demo.setName(name);
		if(null != isDeleted) demo.setIsDeleted(Integer.valueOf(isDeleted));
		return new ModelAndView("demo/demos").addObject("demos", demoService.findByCondition(demo));
	}
	
	@RequestMapping("/demo/findOne4SQL")
	public ModelAndView findOne4SQL(Long id) {
		return new ModelAndView("demo/demos").addObject("demos", demoService.findOne4NativeSQLQuery(id));
	}
	
}
